
/**
 * Admin plug in Script start
 */
(function ( $ ) {
	
	$.fn.henzBind = function(options) {
		var defaults = {
		};
		
		var opts = $.extend({}, defaults, options);
		
		var curFormObj = null;
		var formInputObj = null;
		var curGridObj = null;

		var jsonObj = opts.bindObj;
		var jsonCurStatus = jsonObj.CRUD_STATUS;
		
		curFormObj = $(this);
		//bindGridObj
		
		if(opts.bindGridObj != null){
			curGridObj = opts.bindGridObj;
		};
		
		this.setJsonData = function( pData ){
			jsonObj = pData;
			this.refreshForm();
		};
		
		//$('#excelSheetForm :input' )
		
		this.refreshForm = function (){
			
			jsonCurStatus = jsonObj.CRUD_STATUS;

			for (var jsonKey in jsonObj) {
				formInputObj = null;
				if (jsonObj.hasOwnProperty(jsonKey)) {
					
					if(
						jsonKey.indexOf( '_DATE' ) == ( jsonKey.length - 5 )
					){
						if( ( jsonObj[ jsonKey ] + '' ).length > 10){
							//DB date type value like [1407780255860] 	2014.08.17 by j4.choi
							if( !isNaN(jsonObj[ jsonKey ]) ) {
								jsonObj[ jsonKey ] = new Date(jsonObj[ jsonKey ]);

							} else {
								jsonObj[ jsonKey ] = ( jsonObj[ jsonKey ] + '' ).substring( 0, 10 );
								var dateStrArr = jsonObj[ jsonKey ].split( '-' );
								
								jsonObj[ jsonKey ] = dateStrArr[ 1 ] + '/' + dateStrArr[ 2 ] + '/' + dateStrArr[ 0 ];
							}
						} 
					}
					
					formInputObj = curFormObj.find("[name='" + jsonKey + "']");
					if(formInputObj.prop("tagName") == null || formInputObj.prop("tagName") == "undefined"){
						formInputObj = curFormObj.find("#" + jsonKey + ""); 
					}
					
					if(
						   jsonCurStatus == 'R' 
						&& formInputObj.prop( "type" ) != 'hidden'
					){
						formInputObj.attr( 'disable', true );
						formInputObj.attr( 'disabled', true );
						formInputObj.attr( 'readOnly', true );
					}
					
					if(
						   formInputObj.attr( "keyField" ) == 'true'
						|| formInputObj.attr( "keyField" ) == true
					){
						if( jsonCurStatus != 'C' ){
							formInputObj.attr( 'disable', true );
							formInputObj.attr( 'disabled', true );
							formInputObj.attr( 'readOnly', true );
						}else{
							formInputObj.attr( 'disable', false );
							formInputObj.attr( 'disabled', false );
							formInputObj.attr( 'readOnly', false );
						}
					}
					
					//console.log( "[" + jsonKey + "][" + jsonObj[ jsonKey ] + "][" + formInputObj.prop("tagName") + "]" );
					
					if( 
						   formInputObj.prop("tagName") == 'INPUT'
						|| formInputObj.prop("tagName") == 'SELECT'
						|| formInputObj.prop("tagName") == 'TEXTAREA'
					){
						
						if(jsonObj[ jsonKey ] != null 
							//&& jsonObj[ jsonKey ] != ''
								){
							
							formInputObj.val( jsonObj[ jsonKey ] );
							if(
								jsonKey.indexOf( '_DATE' ) == ( jsonKey.length - 5 )
							){
								if(formInputObj.val()){
									formInputObj.val("");
								}
							}
							
							formInputObj.attr( 'data-default', jsonObj[ jsonKey ] );
							formInputObj.val( jsonObj[ jsonKey ] );
						}
						
						if(formInputObj.prop("tagName") == 'SELECT'){
							
							//formInputObj.attr( 'data-default', jsonObj[ jsonKey ] );														
							try{
								formInputObj.select('setVal', jsonObj[ jsonKey ] );
							}catch(e){}
						}
						
						formInputObj.off('change');
						formInputObj.on('change', function () {
							var curInputObj = $(this);
							var curInputName = curInputObj.attr( 'name' );
							var curVal = curInputObj.val();
							jsonObj[ curInputName ] = curVal;
							if( jsonObj.CRUD_STATUS != 'C' ){
								jsonObj.CRUD_STATUS = 'U';
							}
							if(curGridObj != null){
								
							}
						});
					}else if( 
						   formInputObj.prop("tagName") == 'DIV' 
						|| formInputObj.prop("tagName") == 'SPAN'
					){
						formInputObj.html( jsonObj[ jsonKey ] );
					}
				}
			}
			
		};
		
		this.refreshForm();
		
		
		return this.each(function(){$this = $(this);});
	};
	

}
)(jQuery);
/**
 * Admin plug in Script end
 */


